﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        private IProductRepository Repository { get; set; }
        public int PageSize { get; set; }

        public ProductController( IProductRepository productRepository )
        {
            this.Repository = productRepository;
            this.PageSize = 4;
        }

        public ViewResult List( string category, int page = 1 )
        {
            var model = new ProductListViewModel
            {
                Products = this.Repository.Products
                    .Where( product => category == null || product.Category == category )
                    .OrderBy( product => product.ProductID )
                    .Skip( ( page - 1 ) * this.PageSize )
                    .Take( this.PageSize ),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = this.PageSize,
                    TotalItems = category == null 
                        ? this.Repository.Products.Count()
                        : this.Repository.Products.Where( product => product.Category == category).Count()
                },
                CurrentCategory = category
            };
            return View( model );
        }

    }
}
